﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
using System.Data.SqlClient;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class MatchRatings : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
            //if (!HttpContext.Current.User.Identity.IsAuthenticated)
            //    Response.Redirect("~/Default.aspx");

            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            //this.Title = Resources.Resource.MatchRatings_Title + " - " + GND.ReturnAppTitle();
            
            //if (ou.TeamId == "Null")
            //    Response.Redirect("~/GeneralMessage.aspx?MessageString=M74ggdizz5wfeh");
            if (Request.QueryString["ConfirmNote"] != null)
            {
                this.ConfirmNote.ForeColor = System.Drawing.Color.Green;
                this.ConfirmNote.Text = Request.QueryString["ConfirmNote"].ToString();
            }
            string MatchId = null;
            if (Request.QueryString["MatchId"] != null)
            {
                MatchId = Request.QueryString["MatchId"].ToString();
                //BackToMatchPageLinkButton.PostBackUrl = "MatchPage.aspx?MatchId=" + MatchId;
                this.MatchIdHiddenField.Value = MatchId;
                MatchHyperlink.NavigateUrl = "MatchPage.aspx?MatchId=" + MatchId;
                string MatchHyperlinkText = GND.ReadNivdalStringData("MatchId", "Nivdal_GetMatchTitleString", MatchId);
                MatchHyperlink.Text = MatchHyperlinkText;
                this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.MatchRatings_PageTitle + ": " + MatchHyperlinkText;
                DetailsExistHiddenField.Value = GND.ReadNivdalBoolData("MatchId", "Nivdal_MatchStatsExist", MatchId).ToString();

                if (DetailsExistHiddenField.Value.ToLower() == "false")
                {
                    RatingTabs.Tabs[2].Visible = false;
                }
                else   
                    ReadDetails();
  
            }
            if (MatchId == null)
                Response.Redirect("GeneralMessage.aspx?MessageString=l43ZZOTLOB");
            //this.ConfirmNote.Text=(MatchStage(MatchId));
            if (MatchStage(MatchId) != "2")
                Response.Redirect("GeneralMessage.aspx?MessageString=l43ZZOTLOB");

            if (!IsPostBack)
            {
                string Language = ou.Language;
                this.PostMatchStatisticsSqlDataSource.SelectParameters.Add("MatchId", MatchId);
                this.PostMatchStatisticsSqlDataSource.SelectParameters.Add("Language", Language);
                this.playerRatingsForHomeTeamSqlDataSource.SelectParameters.Add("MatchId", MatchId);
                this.playerRatingsForHomeTeamSqlDataSource.SelectParameters.Add("Language", Language);
                this.playerRatingsForAwayHomeTeamSqlDataSource.SelectParameters.Add("MatchId", MatchId);
                this.playerRatingsForAwayHomeTeamSqlDataSource.SelectParameters.Add("Language", Language);
                this.HomePlayersRatingsSumaryDataSourse.SelectParameters.Add("MatchId", MatchId);
                this.HomePlayersRatingsSumaryDataSourse.SelectParameters.Add("Language", Language);
                this.AwayPlayersRatingsSumaryDataSourse.SelectParameters.Add("MatchId", MatchId);
                this.AwayPlayersRatingsSumaryDataSourse.SelectParameters.Add("Language", Language);
                this.HomeTeamDataSourse.SelectParameters.Add("MatchId", MatchId);
                this.AwayTeamDataSourse.SelectParameters.Add("MatchId", MatchId);
            }

        }


        protected string MatchStage(string MatchId)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            string MS = GND.ReadNivdalIntData("@MatchId", "Nivdal_GetMatchStage", MatchId);
            return MS;
        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }

        //protected void DetailsButtonClick(object sender, EventArgs e)
        //{
        //    DetailsLinkButton.Enabled = false;
        //    DetailsPanel.Visible = true;

        //    SummaryLinkButton.Enabled = true;
        //    SummaryPanel.Visible = false;

        //    PlayersPanel.Visible = false;
        //    PlayersLinkButton.Enabled = true;

        //}

        //protected void SummaryButtonClick(object sender, EventArgs e)
        //{
        //    DetailsPanel.Visible = false;
        //    if (DetailsExistHiddenField.Value.ToLower() == "true")
        //        DetailsLinkButton.Enabled = true;

        //    SummaryPanel.Visible = true;
        //    SummaryLinkButton.Enabled = false;

        //    PlayersPanel.Visible = false;
        //    PlayersLinkButton.Enabled = true;

        //}
        //protected void PlayersButtonClick(object sender, EventArgs e)
        //{
        //    DetailsPanel.Visible = false;
        //    if (DetailsExistHiddenField.Value.ToLower() == "true")
        //        DetailsLinkButton.Enabled = true;

        //    SummaryPanel.Visible = false;
        //    SummaryLinkButton.Enabled = true;

        //    PlayersPanel.Visible = true;
        //    PlayersLinkButton.Enabled = false;
        //}

        private void ReadDetails()
        {
            SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            SqlCommand cmd = new SqlCommand("Nivdal_GetMatchStatistics", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("MatchId", MatchIdHiddenField.Value);

            SqlConnection conn1 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            SqlCommand cmd1 = new SqlCommand("Nivdal_GetMatchTeamIdsAndNames", conn1);
            cmd1.CommandType = CommandType.StoredProcedure;

            cmd1.Parameters.AddWithValue("MatchId", MatchIdHiddenField.Value);

            cmd1.Parameters.Add("HomeTeamId", SqlDbType.Decimal, 12);
            cmd1.Parameters["HomeTeamId"].Direction = ParameterDirection.Output;

            cmd1.Parameters.Add("HomeTeamName", SqlDbType.NVarChar, 256);
            cmd1.Parameters["HomeTeamName"].Direction = ParameterDirection.Output;

            cmd1.Parameters.Add("AwayTeamId", SqlDbType.Decimal, 12);
            cmd1.Parameters["AwayTeamId"].Direction = ParameterDirection.Output;

            cmd1.Parameters.Add("AwayTeamName", SqlDbType.NVarChar, 256);
            cmd1.Parameters["AwayTeamName"].Direction = ParameterDirection.Output;

            try
            {
                conn1.Open();
                cmd1.ExecuteNonQuery();
            }
            finally
            {
                conn1.Close();
            }

            HomeTeamNameLabel.Text = cmd1.Parameters["HomeTeamName"].Value.ToString();
            AwayTeamNameLabel.Text = cmd1.Parameters["AwayTeamName"].Value.ToString();

            string HomeTeamId = cmd1.Parameters["HomeTeamId"].Value.ToString();
            string AwayTeamId = cmd1.Parameters["AwayTeamId"].Value.ToString();


            SqlDataReader Reader;

            try
            {
                conn.Open();
                Reader = cmd.ExecuteReader();
                while (Reader.Read())
                {
                    // 0 is teamId
                    if (Reader.GetDecimal(0).ToString() == HomeTeamId)
                    {
                        // home team stats
                        // (1) is Line (2) is side
                        if (Reader.GetDecimal(1) == 1)
                        {
                            if (Reader.GetDecimal(2) == 1)
                            {
                                // Home 1 1 - 7-th square
                                ShortPassesHomeLabel7.Text = Reader.GetDecimal(3).ToString() + " / " + Reader.GetDecimal(4).ToString();
                                LongPassesHomeLabel7.Text = Reader.GetDecimal(5).ToString() + " / " + Reader.GetDecimal(6).ToString();
                                DribblesHomeLabel7.Text = Reader.GetDecimal(7).ToString() + " / " + Reader.GetDecimal(8).ToString();
                            }
                            else if (Reader.GetDecimal(2) == 2)
                            {
                                // Home 1 2 - 8-th square
                                ShortPassesHomeLabel8.Text = Reader.GetDecimal(3).ToString() + " / " + Reader.GetDecimal(4).ToString();
                                LongPassesHomeLabel8.Text = Reader.GetDecimal(5).ToString() + " / " + Reader.GetDecimal(6).ToString();
                                DribblesHomeLabel8.Text = Reader.GetDecimal(7).ToString() + " / " + Reader.GetDecimal(8).ToString();

                            }
                            else if (Reader.GetDecimal(2) == 3)
                            {
                                // Home 1 3 - 9-th square
                                ShortPassesHomeLabel9.Text = Reader.GetDecimal(3).ToString() + " / " + Reader.GetDecimal(4).ToString();
                                LongPassesHomeLabel9.Text = Reader.GetDecimal(5).ToString() + " / " + Reader.GetDecimal(6).ToString();
                                DribblesHomeLabel9.Text = Reader.GetDecimal(7).ToString() + " / " + Reader.GetDecimal(8).ToString();
                            }
                        }
                        else if (Reader.GetDecimal(1) == 2)
                        {
                            if (Reader.GetDecimal(2) == 1)
                            {
                                // Home 2 1 - 4-th square
                                ShortPassesHomeLabel4.Text = Reader.GetDecimal(3).ToString() + " / " + Reader.GetDecimal(4).ToString();
                                LongPassesHomeLabel4.Text = Reader.GetDecimal(5).ToString() + " / " + Reader.GetDecimal(6).ToString();
                                DribblesHomeLabel4.Text = Reader.GetDecimal(7).ToString() + " / " + Reader.GetDecimal(8).ToString();
                            }
                            else if (Reader.GetDecimal(2) == 2)
                            {
                                // Home 2 2 - 5-th square
                                ShortPassesHomeLabel5.Text = Reader.GetDecimal(3).ToString() + " / " + Reader.GetDecimal(4).ToString();
                                LongPassesHomeLabel5.Text = Reader.GetDecimal(5).ToString() + " / " + Reader.GetDecimal(6).ToString();
                                DribblesHomeLabel5.Text = Reader.GetDecimal(7).ToString() + " / " + Reader.GetDecimal(8).ToString();
                            }
                            else if (Reader.GetDecimal(2) == 3)
                            {
                                // Home 2 3 - 6-th square
                                ShortPassesHomeLabel6.Text = Reader.GetDecimal(3).ToString() + " / " + Reader.GetDecimal(4).ToString();
                                LongPassesHomeLabel6.Text = Reader.GetDecimal(5).ToString() + " / " + Reader.GetDecimal(6).ToString();
                                DribblesHomeLabel6.Text = Reader.GetDecimal(7).ToString() + " / " + Reader.GetDecimal(8).ToString();
                            }
                        }
                        else if (Reader.GetDecimal(1) == 3)
                        {
                            if (Reader.GetDecimal(2) == 1)
                            {
                                // Home 3 1 - 1-th square
                                ShortPassesHomeLabel1.Text = Reader.GetDecimal(3).ToString() + " / " + Reader.GetDecimal(4).ToString();
                                LongPassesHomeLabel1.Text = Reader.GetDecimal(5).ToString() + " / " + Reader.GetDecimal(6).ToString();
                                DribblesHomeLabel1.Text = Reader.GetDecimal(7).ToString() + " / " + Reader.GetDecimal(8).ToString();
                                ShotsHomeLabel1.Text = Reader.GetDecimal(9).ToString() + " / " + Reader.GetDecimal(10).ToString();
                                HeadersHomeLabel1.Text = Reader.GetDecimal(11).ToString() + " / " + Reader.GetDecimal(12).ToString();
                            }
                            else if (Reader.GetDecimal(2) == 2)
                            {
                                // Home 3 2 - 2-th square
                                ShortPassesHomeLabel2.Text = Reader.GetDecimal(3).ToString() + " / " + Reader.GetDecimal(4).ToString();
                                LongPassesHomeLabel2.Text = Reader.GetDecimal(5).ToString() + " / " + Reader.GetDecimal(6).ToString();
                                DribblesHomeLabel2.Text = Reader.GetDecimal(7).ToString() + " / " + Reader.GetDecimal(8).ToString();
                                ShotsHomeLabel2.Text = Reader.GetDecimal(9).ToString() + " / " + Reader.GetDecimal(10).ToString();
                                HeadersHomeLabel2.Text = Reader.GetDecimal(11).ToString() + " / " + Reader.GetDecimal(12).ToString();
                            }
                            else if (Reader.GetDecimal(2) == 3)
                            {
                                // Home 3 3 - 3-th square
                                ShortPassesHomeLabel3.Text = Reader.GetDecimal(3).ToString() + " / " + Reader.GetDecimal(4).ToString();
                                LongPassesHomeLabel3.Text = Reader.GetDecimal(5).ToString() + " / " + Reader.GetDecimal(6).ToString();
                                DribblesHomeLabel3.Text = Reader.GetDecimal(7).ToString() + " / " + Reader.GetDecimal(8).ToString();
                                ShotsHomeLabel3.Text = Reader.GetDecimal(9).ToString() + " / " + Reader.GetDecimal(10).ToString();
                                HeadersHomeLabel3.Text = Reader.GetDecimal(11).ToString() + " / " + Reader.GetDecimal(12).ToString();
                            }
                        }

                    }
                    else
                    {
                        // away team stats
                        if (Reader.GetDecimal(1) == 1)
                        {
                            if (Reader.GetDecimal(2) == 1)
                            {
                                // Away 1 1 - 7-th square
                                ShortPassesAwayLabel7.Text = Reader.GetDecimal(3).ToString() + " / " + Reader.GetDecimal(4).ToString();
                                LongPassesAwayLabel7.Text = Reader.GetDecimal(5).ToString() + " / " + Reader.GetDecimal(6).ToString();
                                DribblesAwayLabel7.Text = Reader.GetDecimal(7).ToString() + " / " + Reader.GetDecimal(8).ToString();
                                ShotsAwayLabel7.Text = Reader.GetDecimal(9).ToString() + " / " + Reader.GetDecimal(10).ToString();
                                HeadersAwayLabel7.Text = Reader.GetDecimal(11).ToString() + " / " + Reader.GetDecimal(12).ToString();
                            }
                            else if (Reader.GetDecimal(2) == 2)
                            {
                                // Away 1 2 - 8-th square
                                ShortPassesAwayLabel8.Text = Reader.GetDecimal(3).ToString() + " / " + Reader.GetDecimal(4).ToString();
                                LongPassesAwayLabel8.Text = Reader.GetDecimal(5).ToString() + " / " + Reader.GetDecimal(6).ToString();
                                DribblesAwayLabel8.Text = Reader.GetDecimal(7).ToString() + " / " + Reader.GetDecimal(8).ToString();
                                ShotsAwayLabel8.Text = Reader.GetDecimal(9).ToString() + " / " + Reader.GetDecimal(10).ToString();
                                HeadersAwayLabel8.Text = Reader.GetDecimal(11).ToString() + " / " + Reader.GetDecimal(12).ToString();
                            }
                            else if (Reader.GetDecimal(2) == 3)
                            {
                                // Away 1 3 - 9-th square
                                ShortPassesAwayLabel9.Text = Reader.GetDecimal(3).ToString() + " / " + Reader.GetDecimal(4).ToString();
                                LongPassesAwayLabel9.Text = Reader.GetDecimal(5).ToString() + " / " + Reader.GetDecimal(6).ToString();
                                DribblesAwayLabel9.Text = Reader.GetDecimal(7).ToString() + " / " + Reader.GetDecimal(8).ToString();
                                ShotsAwayLabel9.Text = Reader.GetDecimal(9).ToString() + " / " + Reader.GetDecimal(10).ToString();
                                HeadersAwayLabel9.Text = Reader.GetDecimal(11).ToString() + " / " + Reader.GetDecimal(12).ToString();
                            }
                        }
                        else if (Reader.GetDecimal(1) == 2)
                        {
                            if (Reader.GetDecimal(2) == 1)
                            {
                                // Away 2 1 - 4-th square
                                ShortPassesAwayLabel4.Text = Reader.GetDecimal(3).ToString() + " / " + Reader.GetDecimal(4).ToString();
                                LongPassesAwayLabel4.Text = Reader.GetDecimal(5).ToString() + " / " + Reader.GetDecimal(6).ToString();
                                DribblesAwayLabel4.Text = Reader.GetDecimal(7).ToString() + " / " + Reader.GetDecimal(8).ToString();
                            }
                            else if (Reader.GetDecimal(2) == 2)
                            {
                                // Away 2 2 - 5-th square
                                ShortPassesAwayLabel5.Text = Reader.GetDecimal(3).ToString() + " / " + Reader.GetDecimal(4).ToString();
                                LongPassesAwayLabel5.Text = Reader.GetDecimal(5).ToString() + " / " + Reader.GetDecimal(6).ToString();
                                DribblesAwayLabel5.Text = Reader.GetDecimal(7).ToString() + " / " + Reader.GetDecimal(8).ToString();
                            }
                            else if (Reader.GetDecimal(2) == 3)
                            {
                                // Away 2 3 - 6-th square
                                ShortPassesAwayLabel6.Text = Reader.GetDecimal(3).ToString() + " / " + Reader.GetDecimal(4).ToString();
                                LongPassesAwayLabel6.Text = Reader.GetDecimal(5).ToString() + " / " + Reader.GetDecimal(6).ToString();
                                DribblesAwayLabel6.Text = Reader.GetDecimal(7).ToString() + " / " + Reader.GetDecimal(8).ToString();
                            }
                        }
                        else if (Reader.GetDecimal(1) == 3)
                        {
                            if (Reader.GetDecimal(2) == 1)
                            {
                                // Away 3 1 - 1-th square
                                ShortPassesAwayLabel1.Text = Reader.GetDecimal(3).ToString() + " / " + Reader.GetDecimal(4).ToString();
                                LongPassesAwayLabel1.Text = Reader.GetDecimal(5).ToString() + " / " + Reader.GetDecimal(6).ToString();
                                DribblesAwayLabel1.Text = Reader.GetDecimal(7).ToString() + " / " + Reader.GetDecimal(8).ToString();
                            }
                            else if (Reader.GetDecimal(2) == 2)
                            {
                                // Away 3 2 - 2-th square
                                ShortPassesAwayLabel2.Text = Reader.GetDecimal(3).ToString() + " / " + Reader.GetDecimal(4).ToString();
                                LongPassesAwayLabel2.Text = Reader.GetDecimal(5).ToString() + " / " + Reader.GetDecimal(6).ToString();
                                DribblesAwayLabel2.Text = Reader.GetDecimal(7).ToString() + " / " + Reader.GetDecimal(8).ToString();
                            }
                            else if (Reader.GetDecimal(2) == 3)
                            {
                                // Away 3 3 - 3-th square
                                ShortPassesAwayLabel3.Text = Reader.GetDecimal(3).ToString() + " / " + Reader.GetDecimal(4).ToString();
                                LongPassesAwayLabel3.Text = Reader.GetDecimal(5).ToString() + " / " + Reader.GetDecimal(6).ToString();
                                DribblesAwayLabel3.Text = Reader.GetDecimal(7).ToString() + " / " + Reader.GetDecimal(8).ToString();
                            }
                        }
                    }

                }
                Reader.Close();
            }
            finally
            {
                conn.Close();
            }

        }


        protected ContentDirection OppositeDirection()
        {
            ContentDirection dir = (Master.Master.FindControl("DirectionPanel") as Panel).Direction;
            ContentDirection OppositeDir;
            if (dir == ContentDirection.LeftToRight)
                OppositeDir = ContentDirection.RightToLeft;
            else
                OppositeDir = ContentDirection.LeftToRight;

            return OppositeDir;
        }
    }
}